<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .myImg{
            width: 200px;
            height: 200px;
            background-image: url(./imgs/1.webp);
        }

    </style>
</head>
<body>
    <div class="myImg"></div>
    <button>点击</button>
    <script>
        var arr = ['./imgs/1.webp','./imgs/2.webp','./imgs/3.webp'];
        var btn = document.getElementsByTagName("button")[0];
        // console.log(btn);
        // btn.onclick = function(){
        //     console.log(123);
        // }
        var myImgEle = document.querySelector(".myImg");
        function randomNum(min,max){
            return Math.round(Math.random()*(max-min)+min);
        }
        // 随机图片的地址；
        // console.log(arr[randomNum(0,2)]);
        btn.onclick = function(){
            var str =  "url("+(arr[randomNum(0,2)])+")";
            console.log(str);
            myImgEle.style.backgroundImage = "url("+(arr[randomNum(0,2)])+")";
        }
        

    </script>
</body>
</html>